<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="视频名称" prop="videoName">
      <el-input v-model="dataForm.videoName" placeholder="视频名称"></el-input>
    </el-form-item>
    <el-form-item label="IP地址" prop="ipAddress">
      <el-input v-model="dataForm.ipAddress" placeholder="IP地址"></el-input>
    </el-form-item>
    <el-form-item label="用户名" prop="username">
      <el-input v-model="dataForm.username" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="端口号" prop="portNumber">
      <el-input v-model="dataForm.portNumber" placeholder="端口号"></el-input>
    </el-form-item>
    <el-form-item label="编码方式" prop="encodingMethod">
      <el-input v-model="dataForm.encodingMethod" placeholder="编码方式"></el-input>
    </el-form-item>
    <el-form-item label="码率" prop="codeRate">
      <el-input v-model="dataForm.codeRate" placeholder="码率"></el-input>
    </el-form-item>
    <el-form-item label="设备位置" prop="codeLocation">
      <el-input v-model="dataForm.codeLocation" placeholder="设备位置"></el-input>
    </el-form-item>
    <el-form-item label="实时视频" prop="realTimeVideo">
      <el-input v-model="dataForm.realTimeVideo" placeholder="实时视频"></el-input>
    </el-form-item>


   <!-- <el-form-item label="创建时间" prop="createTime">
     <el-date-picker
          v-model="dataForm.createTime"
          type="datetime"
          placeholder="选择日期时间">
        </el-date-picker>
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-date-picker
           v-model="dataForm.updateTime"
           type="datetime"
           placeholder="选择日期时间">
         </el-date-picker>
       <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="0正常 其余删除" prop="delFlag">
      <el-input v-model="dataForm.delFlag" placeholder="0正常 其余删除"></el-input>
    </el-form-item> -->
    <el-form-item label="设备类型" prop="codeType">
      <el-input v-model="dataForm.codeType" placeholder="设备类型"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj} from '@/api/hik/video'

    export default {
    data () {
      return {
        visible: false,
        canSubmit: false,
        dataForm: {
          videoId: 0,
          videoName: '',

          ipAddress: '',

          username: '',

          password: '',

          portNumber: '',

          encodingMethod: '',

          codeRate: '',

          codeLocation: '',

          realTimeVideo: '',

          createTime: '',

          updateTime: '',

          delFlag: '',

          codeType: '',

        },
        dataRule: {
          videoName: [
            { required: true, message: '视频名称不能为空', trigger: 'blur' }
          ],

          ipAddress: [
            { required: true, message: 'IP地址不能为空', trigger: 'blur' }
          ],

          username: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],

          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],

          portNumber: [
            { required: true, message: '端口号不能为空', trigger: 'blur' }
          ],

          encodingMethod: [
            { required: true, message: '编码方式不能为空', trigger: 'blur' }
          ],

          codeRate: [
            { required: true, message: '码率不能为空', trigger: 'blur' }
          ],

          codeLocation: [
            { required: true, message: '设备位置不能为空', trigger: 'blur' }
          ],

          realTimeVideo: [
            { required: true, message: '实时视频不能为空', trigger: 'blur' }
          ],

         /* createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],

          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ], */

         /* delFlag: [
            { required: true, message: '0正常 其余删除不能为空', trigger: 'blur' }
          ], */

          codeType: [
            { required: true, message: '设备类型不能为空', trigger: 'blur' }
          ],

        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.videoId = id || 0;
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.videoId) {
            getObj(this.dataForm.videoId).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {

        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.videoId) {
                putObj(this.dataForm).then(data => {
                    this.$notify.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            } else {
                addObj(this.dataForm).then(data => {
                    this.$notify.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            }
          }
        })
      }
    }
  }
</script>
